﻿using System;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using WiMo.Games.Drawables;
using WiMo.Games.Drawing.Silverlight;

namespace WiMo.Games.Drawing.WP7S.Drawables
{
    public class LineList : SpriteShapeBase
    {
        Canvas _lineList;

        public LineList()
        {
            ShapeType = WiMo.Games.Drawables.ShapeType.LineList;
            Origin = WiMo.Games.Drawables.Origin.TopLeft;
        }

        public override void Render(IDisplay display)
        {
            _lineList = new Canvas();
         
            for(var idx = 0; idx < Points.Count - 1; ++idx)
            {
                var lines = new System.Windows.Media.LineGeometry();
                lines.StartPoint = new System.Windows.Point(Points[idx].X, Points[idx].Y);
                lines.EndPoint = new System.Windows.Point(Points[idx + 1].X, Points[idx + 1].Y);

                var path = new Path();
                if (this.BorderColor != Color.Transparent)
                {
                    path.Stroke = new System.Windows.Media.SolidColorBrush(this.BorderColor.GetMediaColor());
                    path.StrokeThickness = this.BorderWidth;
                }

                path.Data = lines;

                _lineList.Children.Add(path);
            }

            TransformGroup tg = new TransformGroup();
            var translate = new TranslateTransform();
            var rotate = new RotateTransform();
            var scale = new ScaleTransform();

            tg.Children.Add(translate);
            tg.Children.Add(rotate);
            tg.Children.Add(scale);

            _lineList.RenderTransform = tg;

            if(Origin == WiMo.Games.Drawables.Origin.Center)
            {
                _lineList.SetValue(Canvas.TopProperty, (double)Position.Y - Size.Height / 2);
                _lineList.SetValue(Canvas.LeftProperty, (double)Position.X - Size.Width / 2);
            }
            else
            {
                _lineList.SetValue(Canvas.TopProperty, (double)Position.Y);
                _lineList.SetValue(Canvas.LeftProperty, (double)Position.X);                
            }

            DrawInformation = _lineList;

            display.Render(this);
        }
    }
}
